/** 
* @author : 褚国强
* @date 创建时间：2021年11月17日 上午10:07:27 
* @version 1.0
*/
package com.bsoft.project.service;

import java.io.File;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsoft.project.mapper.RsfComlfsServiceMapper;
import com.bsoft.project.utils.Decompressing;
import com.bsoft.project.utils.RsfComIfsService;

/** 
* @author : 褚国强
* @date 创建时间：2021年11月17日 上午10:07:27 
* @Description: TODO
* @version 1.0 
* @parameter 
* @since 
* @return 
*/
@Service
public class RsfComlfServiceSvc {
	
	@Value("${rsf-comlf.url}") private String RsfComlUrl;
	@Value("${rsf-comlf.key}") private String key;
	@Value("${rsf-comlf.secret}") private String secret;
	
	@Autowired
    private RsfComlfsServiceMapper rsfComlfsServiceMapper;
	
	private final Logger logger = LoggerFactory.getLogger(RsfComlfServiceSvc.class);

	@Transactional(rollbackFor = Exception.class)
	public HashMap<String, Object> rsfComIfsService(HashMap<String,Object> param) throws Exception {
		
		String parStr = JSON.toJSONString(param);
		File fileDir = new File("\\gjyb_down"); 
		if(!fileDir.exists()){
			fileDir.mkdirs(); 
		}
		String type = param.get("download_type").toString(); // 下载类型
		long beginTime = System.currentTimeMillis();
		
		String filePath = "\\gjyb_down\\" + param.get("fixmedins_code")+ "_" + type + "_" + System.currentTimeMillis() + ".zip";
		
		RsfComIfsService.sentService(RsfComlUrl, parStr, filePath, key, secret);
		
		HashMap<String, Object> returnMap = new HashMap<String, Object>();
		
		returnMap.put("code", 200);
		returnMap.put("message", "SUCCESS!");

		logger.info("---------------------------- read file ");
		
		String zipPath = Decompressing.zipUncompress(filePath);
//		Decompressing.readFiles(zipPath);
		readFiles(zipPath,type);
//		readFiles("D:\\gjyb_down\\H33048300087_1637223928670");
		
		long endTime = System.currentTimeMillis();
		logger.info("---------------------------- 总时间（毫秒）：" + (endTime - beginTime));
		return returnMap;
	}
	/**
	 * 解压文件夹解析
	*/
	private void readFiles(String inputFile,String type) throws Exception {
		File srcFile = new File(inputFile);
		if (srcFile.isDirectory()) {
			File next[] = srcFile.listFiles();
			for (int i = 0; i < next.length; i++) {
				logger.info(next[i].getName());
				if (!next[i].isDirectory()) {

					LineIterator lineIterator = FileUtils.lineIterator(next[i], "utf-8");
					long beginTime = System.currentTimeMillis();
//					List<String[]> arr1 = new ArrayList<>();
					int lineNum = 0;
					while (lineIterator.hasNext()) {
						String result = lineIterator.next();
						String[] arr = result.split("\t");
						try{
							insertOracle(arr,type);							
						}catch(Exception e){
							for(int a=0;a<arr.length;a++){
								logger.error("解析失败---------"+ (a+1) +"--------" + arr[a]);
							}
							throw new Exception(e);
						}
						if(lineNum%5000 == 0){
							logger.info("已解析-----------------" + lineNum);
							
						}
						lineNum ++;
					}
					logger.info("总解析-----------------" + lineNum);
					long endTime = System.currentTimeMillis();
					logger.info("" + (endTime - beginTime));


				}
			}
		}
	}
	
	private void insertOracle(String[] arr,String type){
		if(type.equals("1301")){   // --------------西药中成药目录下载
			if(arr[18].length()>2000){
				arr[18] = arr[18].substring(0, 2000);								
			}
			rsfComlfsServiceMapper.insertYbmlStr(arr);	
		}else if(type.equals("1302")){  // --------------中药饮片目录下载
			rsfComlfsServiceMapper.insertZyypml(arr);	
		}else if(type.equals("1303")){  // --------------医疗机构制剂目录下载
			rsfComlfsServiceMapper.insertYljgzjml(arr);	
		}else if(type.equals("1304")){  // --------------民族药品目录查询
			
		}else if(type.equals("1305")){  // --------------医疗服务项目目录下载
			rsfComlfsServiceMapper.insertYlfwxmml(arr);	
		}else if(type.equals("1306")){  // --------------医用耗材目录下载
			rsfComlfsServiceMapper.insertYyhcml(arr);
		}else if(type.equals("1307")){  // --------------疾病与诊断目录下载
			rsfComlfsServiceMapper.insertJbyzdml(arr);
		}else if(type.equals("1308")){  // --------------手术操作目录下载
			rsfComlfsServiceMapper.insertSsczml(arr);
		}else if(type.equals("1309")){  // --------------门诊慢特病种目录下载
			rsfComlfsServiceMapper.insertMztmbzml(arr);
		}else if(type.equals("1310")){  // --------------按病种付费病种目录下载
			rsfComlfsServiceMapper.insertAbzffbzml(arr);
		}else if(type.equals("1311")){  // --------------日间手术治疗病种目录下载   502错误中
			rsfComlfsServiceMapper.insertRjsszlbzml(arr);
		}else if(type.equals("1312")){  // --------------医保目录信息查询
			
		}else if(type.equals("1313")){  // --------------肿瘤形态学目录下载
			rsfComlfsServiceMapper.insertZlxtxml(arr);
		}else if(type.equals("1314")){  // --------------中医疾病目录下载
			rsfComlfsServiceMapper.insertZyjbml(arr);
		}else if(type.equals("1315")){  // --------------中医证候目录下载
			rsfComlfsServiceMapper.insertZyzhml(arr);
		}else if(type.equals("1316")){  // --------------医疗目录与医保目录匹配信息查询
			
		}else if(type.equals("1317")){  // --------------医药机构目录匹配信息查询
			
		}else if(type.equals("1318")){  // --------------医保目录限价信息查询
			
		}else if(type.equals("1319")){  // --------------医保目录先自付比例信息查询
			
		}
		
	}
	
}
